package com.oa.controller.user;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.oa.pojo.Task;
import com.oa.service.admin.AdminService;
import com.oa.service.user.TaskService;
import com.oa.vo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

@RestController
@CrossOrigin
@RequestMapping("/task")
public class TaskController {

    @Autowired
    private TaskService taskService;

    @Autowired
    private AdminService adminService;

    /**
     * 分页查询所有任务
     */
    @GetMapping("findAll")
    public SysResult findAll() {
        return SysResult.success(taskService.findAll());
    }

    @GetMapping("/getTaskList")
    public SysResult getTaskList(PageResult pageResult) {
        Page<TaskVO> page = new Page<>(pageResult.getPageNum(), pageResult.getPageSize());
        page = taskService.getTaskList(page, pageResult);
        pageResult.setRows(page.getRecords()).setTotal(page.getTotal());
        return SysResult.success(pageResult);
    }

    @GetMapping("/getPerformList")
    public SysResult getPerformList(PageResult pageResult) {
        Page<TaskVO> page = new Page<>(pageResult.getPageNum(), pageResult.getPageSize());
        page = taskService.getPerformList(page, pageResult);
        pageResult.setRows(page.getRecords()).setTotal(page.getTotal());
        return SysResult.success(pageResult);
    }

    @GetMapping("/getTaskInfo")
    public SysResult getTaskInfo(Task task) {

        return SysResult.success(taskService.getTaskInfo(task));
    }

    /**
     * 新增任务
     */
    @PostMapping("/addTask")
    public SysResult addTask(@RequestBody Task task) {
        taskService.addTask(task);
        return SysResult.success();
    }

    /**
     * 修改任务
     */
    @PutMapping("/updateTask")
    public SysResult updateTask(@RequestBody Task task) {
        taskService.updateTask(task);
        return SysResult.success();
    }

    /**
     * 删除任务
     */
    @DeleteMapping("/deleteTask/{id}")
    public SysResult deleteTask(Task task) {
        taskService.deleteTask(task);
        return SysResult.success();
    }

    @GetMapping("/getDepList/{level}")
    public SysResult getDepList(@PathVariable Integer level) {
        return SysResult.success(adminService.getDepartmentInfo(level));
    }

    @GetMapping("/getEmpList/{depId}")
    public SysResult getEmpListByDep(@PathVariable Integer depId) {
        return SysResult.success(taskService.getEmpListByDep(depId));
    }

    @PostMapping("/uploadTaskFile")
    public SysResult taskFileUpload(MultipartFile file) throws IOException {
        TaskFileUploadVO fileUploadVo = taskService.uploadTaskFile(file);
        if (fileUploadVo.getUrlPath().equals("201")) {
            return SysResult.fail();
        }
        return SysResult.success(fileUploadVo);
    }

    @DeleteMapping("/deleteTaskFile")
    public SysResult taskFileDelete(String urlPath, String dateStr) {
        urlPath = urlPath.substring(urlPath.lastIndexOf("/") + 1);
        urlPath = dateStr + "/" + urlPath;
        Integer status = taskService.deleteTaskFile(urlPath);
        if (status != 200) {
            return SysResult.fail(status);
        }
        return SysResult.success(status);
    }

    @GetMapping("/getTaskFile/{taskId}")
    public SysResult getTaskFile(@PathVariable Integer taskId) {
        return SysResult.success(taskService.getTaskFile(taskId));
    }

    @GetMapping("/getTaskInfo/{id}")
    public SysResult getTaskInfo(@PathVariable Integer id) {
        return SysResult.success(taskService.getCurrentTask(id));
    }
}
